/*==============================================================================
             Copyright (c) 2009 - All rights reserved
================================================================================
File description:

    XMLParser function definition

================================================================================
      Date            Name                    Description of Change
15-Sep-2009  Xiaofeng.Gu                    Written
26-Sep-2009 Xiaofeng.Gu                     Add methods : FindXMLNode, FindChildNode
$HISTORY$
==============================================================================*/
#include "stdafx.h"
#include "XMLParser.h"

#define ATTRIBUTE_NAME			_T("Name")
#define ATTRIBUTE_TYPE			_T("Type")
#define ATTRIBUTE_ACTIVE				_T("Active")

CXMLParser::CXMLParser()
{
}

CXMLParser::~CXMLParser()
{
}

void CXMLParser::AddXMLNode(LPCTSTR szNode, LPCTSTR szData)
{
	m_oMarkup.AddElem(szNode, szData);
}

void CXMLParser::AddXMLNode(LPCTSTR szNode, double dData)
{
	CString strData;
	strData.Format(_T("%lf"), dData);
	m_oMarkup.AddElem(szNode, strData);
}

void CXMLParser::AddXMLNode(LPCTSTR szNode, long lData)
{
	CString strData;
	strData.Format(_T("%ld"), lData);
	m_oMarkup.AddElem(szNode, strData);
}

void CXMLParser::AddXMLNode(LPCTSTR szNode, bool bData)
{
	CString strData;
	strData.Format(_T("%d"), bData);
	m_oMarkup.AddElem(szNode, strData);
}

void CXMLParser::AddChildNode(LPCTSTR szChildNode, double dData)
{
	m_oMarkup.IntoElem();
	AddXMLNode(szChildNode, dData);
}

void CXMLParser::AddChildNode(LPCTSTR szChildNode, long lData)
{
	m_oMarkup.IntoElem();
	AddXMLNode(szChildNode, lData);
}

void CXMLParser::AddChildNode(LPCTSTR szChildNode, bool bData)
{
	m_oMarkup.IntoElem();
	AddXMLNode(szChildNode, bData);
}

void CXMLParser::AddChildNode(LPCTSTR szChildNode, LPCTSTR szData)
{
	m_oMarkup.IntoElem();
	AddXMLNode(szChildNode, szData);
}

void CXMLParser::AddAttribute(LPCTSTR szAttrib, LPCTSTR szValue)
{
	m_oMarkup.AddAttrib(szAttrib, szValue);
}

CString CXMLParser::GetAttribute(LPCTSTR szAttrib)
{
	return m_oMarkup.GetAttrib(szAttrib);
}

void CXMLParser::EndXMLNode()
{
	m_oMarkup.OutOfElem();
}

void CXMLParser::AddType(int iType)
{
	CString strAttribue;
	strAttribue.Format(_T("%d"), iType);
	AddAttribute(ATTRIBUTE_TYPE, strAttribue);
}

int CXMLParser::GetType()
{
	return atoi((LPSTR)(LPCTSTR)GetAttribute(ATTRIBUTE_TYPE));
}

void CXMLParser::AddName(LPCTSTR szName)
{
	AddAttribute(ATTRIBUTE_NAME, szName);
}

CString CXMLParser::GetName()
{
	return GetAttribute(ATTRIBUTE_NAME);
}

void CXMLParser::AddActive(bool bActive)
{
	CString strAttribue;
	strAttribue.Format(_T("%d"), bActive);
	AddAttribute(ATTRIBUTE_ACTIVE, strAttribue);
}

bool CXMLParser::GetActive()
{
	bool bRe;
	int iData = atoi((LPSTR)(LPCTSTR)GetAttribute(ATTRIBUTE_ACTIVE));
	iData == (int)(true) ? bRe = true : bRe = false;

	return bRe;
}

CString CXMLParser::GetDoc()
{
	return m_oMarkup.GetDoc();
}

bool CXMLParser::FindXMLNode(LPCTSTR szNode)
{
	if (m_oMarkup.FindElem(szNode))
	{
		return true;
	}
	else
	{
		m_oMarkup.OutOfElem();
		return false;
	}
}

bool CXMLParser::FindXMLNode(LPCTSTR szNode, CString& strData)
{
	if(m_oMarkup.FindElem(szNode))
	{
		strData = m_oMarkup.GetData();

		return true;
	}

	return false;
}

bool CXMLParser::FindXMLNode(LPCTSTR szNode, double& dData)
{
	CString strData;
	if (FindXMLNode(szNode, strData))
	{
		dData = _wtof((LPCTSTR)strData);

		return true;
	}

	return false;
}

bool CXMLParser::FindXMLNode(LPCTSTR szNode, long& lData)
{
	CString strData;
	if (FindXMLNode(szNode, strData))
	{
		lData = atol((LPSTR)(LPCTSTR)strData);

		return true;
	}

	return false;
}

bool CXMLParser::FindXMLNode(LPCTSTR szNode, bool& bData)
{
	CString strData;
	if (FindXMLNode(szNode, strData))
	{
		int iData = atoi((LPSTR)(LPCTSTR)strData);
		iData == (int)(true) ? bData = true : bData = false;

		return true;
	}

	return false;
}


bool CXMLParser::FindChildNode(LPCTSTR szChildNode, CString& strData)
{
	m_oMarkup.IntoElem();
	return FindXMLNode(szChildNode, strData);
}

bool CXMLParser::FindChildNode(LPCTSTR szChildNode, double& dData)
{
	m_oMarkup.IntoElem();
	return FindXMLNode(szChildNode, dData);
}

bool CXMLParser::FindChildNode(LPCTSTR szChildNode, long& lData)
{
	m_oMarkup.IntoElem();
	return FindXMLNode(szChildNode, lData);
}

bool CXMLParser::FindChildNode(LPCTSTR szChildNode, bool& bData)
{
	m_oMarkup.IntoElem();
	return FindXMLNode(szChildNode, bData);
}

bool CXMLParser::FindChildNode(LPCTSTR szChildNode)
{
	m_oMarkup.IntoElem();
	return FindXMLNode(szChildNode);
}